package com.yao2san.simlog.aop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Aspect
public class PrintLogAspect {
    @Pointcut("@annotation(com.yao2san.simlog.annotation.PrintLog)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object execute(ProceedingJoinPoint joinPoint) {
        Logger logger = LoggerFactory.getLogger(joinPoint.getTarget().getClass());
        logger.info("In param:{}", JSONObject.toJSONString(joinPoint.getArgs()));
        try {
            Object obj = joinPoint.proceed();
            long start = System.currentTimeMillis();
            logger.info("Out param:{}", JSONObject.toJSONString(obj));
            long end = System.currentTimeMillis();
            logger.info("Use time:{}ms",(end-start));
            return obj;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }
}
